const query = require("../db/query");

//客服上线
const insertServerOnLine = (data) => {
    const sql = `INSERT INTO servers (uuid,sid,nick,ip,status) VALUES ('${data.uid}','${data.sid}','${data.nick}','${data.ip}','${data.status}');`;
    // console.log('保存新上线客服', data.uid, data.sid)
    return query(sql);
}

//客服离线
const updateServerStatus = (uuid, sid) => {
    const sql = `UPDATE servers SET status='0' WHERE uuid = '${uuid}' AND sid='${sid}' AND status =1;`;
    // console.log('客服离线更新完成', uuid, sid)
    return query(sql);
}

//查询在线客服
const queryOnLineSever = () => {
    const sql = `SELECT * FROM servers WHERE status = 1 ORDER BY servers.id DESC;`;
    let res_sql = query(sql);
    // console.log('查询在线客服',res_sql) //必须通过promise异步构造函数才能读取
    return res_sql
}

//写入聊天记录
const insertChat = (data) => { // ,'${data.nick}'
    console.log('保存聊天记录', data)
    const sql = `INSERT INTO day_yeild (0809,0910,1011,1112,1213,1314,1415) VALUES (123123,123,123,123,123,12321,234324);`;
    // console.log('插入新聊天纪录')
    return query(sql);
}


//写入聊天记录
const clreanServerClinet = (data) => { // ,'${data.nick}'
    const sql = `UPDATE servers SET status='0' WHERE  status =1;`;
    const sql2 = `UPDATE clients SET status='0' WHERE  status =1;`;
    // console.log('清除残留客户和客服（异常停机）')
    // query(sql2)
    return query(sql), query(sql2)
}

//写入/产量记录
const dayInsert = (table,address,JsonData) => { // ,'${data.nick}'
    //  var  address="date,H0809,H0910,H1011,H1112,H1213,H1314,H1415,H1516,H1617,H1718,H1819,H1920,allh"
    //  var table="dayyeild"
    //  var JsonData=["2020-09-13",1323,3343,1011,233,333,1314,1415,1516,1617,1718,1819,1920,32833]
    // const sql = `INSERT INTO dayyeild (Hb,Hj) VALUES ('123123','123');`;
    // const sql = `INSERT INTO servers (uuid,sid,nick,ip,status) VALUES ('${data.uid}','${data.sid}','${data.nick}','${data.ip}','${data.status}');`;
    // const sql = `INSERT INTO `+table+` (`+address+`) VALUES ("2020-09-13",1323,3343,1011,233,333,1314,1415,1516,1617,1718,1819,1920,32333);`;
    const sql = `INSERT INTO `+table+` (`+address+`) VALUES ("`+JsonData[0]+`",`+JsonData[1]+`,`+JsonData[2]+`,`+JsonData[3]+`,`+JsonData[4]+`,`+JsonData[5]+`,`+JsonData[6]+`,`+JsonData[7]+`,`+JsonData[8]+`,`+JsonData[9]+`,`+JsonData[10]+`,`+JsonData[11]+`,`+JsonData[12]+`,`+JsonData[13]+`);`;
    
    console.log('保存生产记录：', address)
   
    // console.log('插入新聊天纪录
 
    return query(sql);
}




module.exports = {
    insertServerOnLine,
    updateServerStatus,
    queryOnLineSever,
    insertChat,
    clreanServerClinet,
    dayInsert

}